/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.DAO;

import com.aptech.model.cash;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Hoang Quy Hong An
 */
public class cashDAO {

    private  Connection myCon = null;
    private  PreparedStatement ps = null;
    private  ResultSet rs = null;
    private String strCommand = "";

    public  boolean lapthe(cash objCash) {
        boolean flag = false;
        strCommand = "UPDATE tbl_Cash SET userID  = ? , cashStatus  = 0 WHERE cashID = ?";
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, objCash.getUserID());
            ps.setInt(2, objCash.getCashID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public  boolean checkCash(String seriesNumber) {
        boolean flag = false;
        int countCash = 0;
        strCommand = "SELECT COUNT(*) AS [countCash] FROM tbl_Cash WHERE cashSeries = ? AND cashStatus = 1";
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, seriesNumber);
            rs = ps.executeQuery();
            if (rs.next()) {
                countCash = rs.getInt("countCash");
                if (countCash > 0) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return flag;
    }

    public  int getCashID(String seriesNumber) {
        int cashID =  0;
        strCommand = "SELECT * FROM tbl_Cash WHERE cashSeries = ?";
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, seriesNumber);
            rs = ps.executeQuery();
            if (rs.next()) {
                cashID = rs.getInt("cashID");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return cashID;
    }

     public  ArrayList getCashByUserID(String userID) {
        ArrayList listCash = null;
        String cashSiries = "";
        strCommand = "SELECT cashSeries FROM tbl_Cash WHERE userID = ?";
        try {
            listCash = new ArrayList();
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, userID);
            rs = ps.executeQuery();
            while (rs.next()) {
                cashSiries = rs.getString("cashSeries");
                listCash.add(cashSiries);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listCash;
    }
}
